package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import net.miginfocom.swing.MigLayout;

public class TableSali extends JPanel {

	private JTextField punto = new JTextField(){
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private String[] values = { "", "critico", "poco critico", "irrilevante" };

	private JComboBox cloruri = new JComboBox(values);

	private JComboBox nitrati = new JComboBox(values);

	private JComboBox solfati = new JComboBox(values);

	private JButton delRilevazione = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("delete.png")));

	private int count;

	public TableSali(final Sali s, final int n) {
		super();
		count = n;
		setLayout(new MigLayout());
		setOpaque(false);
		punto.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		punto.setFont(new Font("Arial", Font.PLAIN, 16));
		punto.setOpaque(false);
		punto.setPreferredSize(new Dimension(150, 25));
		cloruri.setPreferredSize(new Dimension(166, 25));
		nitrati.setPreferredSize(new Dimension(166, 25));
		solfati.setPreferredSize(new Dimension(166, 25));
		delRilevazione.setBorder(null);
		delRilevazione.setContentAreaFilled(false);
		delRilevazione.setRolloverEnabled(true);
		delRilevazione.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("bdelete.png")));
		delRilevazione.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int res = JOptionPane.showConfirmDialog(null,
						"Eliminare valori?", "Attenzione", 0);
				if (res == 0) {

					s.removeTabel(count);
				}
			}
		});
		cloruri.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (cloruri.getSelectedIndex() == 1) {
					cloruri.setBackground(Color.RED);
				} else if (cloruri.getSelectedIndex() == 2) {
					cloruri.setBackground(Color.YELLOW);
				} else if (cloruri.getSelectedIndex() == 3) {
					cloruri.setBackground(Color.GREEN);
				}

			}
		});
		nitrati.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (nitrati.getSelectedIndex() == 1) {
					nitrati.setBackground(Color.RED);
				} else if (nitrati.getSelectedIndex() == 2) {
					nitrati.setBackground(Color.YELLOW);
				} else if (nitrati.getSelectedIndex() == 3) {
					nitrati.setBackground(Color.GREEN);
				}

			}
		});
		solfati.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (solfati.getSelectedIndex() == 1) {
					solfati.setBackground(Color.RED);
				} else if (solfati.getSelectedIndex() == 2) {
					solfati.setBackground(Color.YELLOW);
				} else if (solfati.getSelectedIndex() == 3) {
					solfati.setBackground(Color.GREEN);
				}

			}
		});
		add(delRilevazione);
		add(punto, "gapleft 10");
		add(cloruri, "gapleft 3");
		add(nitrati, "gapleft 3");
		add(solfati, "gapleft 3");

	}

	public String print() {
		String print = "";
		print = print + "\n#" + punto.getText();
		print = print + "\n#" + cloruri.getSelectedIndex();
		print = print + "\n#" + nitrati.getSelectedIndex();
		print = print + "\n#" + solfati.getSelectedIndex();
		return print;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		// System.out.println("[[[[[\n" + data);
		// c.nextLine();

		punto.setText(c.nextLine());

		String tmp = c.nextLine();
		try {
			cloruri.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			// todo
		}
		tmp = c.nextLine();
		try {
			nitrati.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			// TODO
		}
		tmp = c.nextLine();
		try {
			solfati.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			// TODO
		}

	}

	public void setCount(int counts) {
//		System.out.println("set : old-> " + count + " new-> " + counts);
		this.count = counts;
	}

	public Document pdf(Document doc) {

		PdfPTable table = new PdfPTable(7);
		int[] size = { 30, 5, 25, 5, 25, 5, 25 };
		try {
			table.setWidths(size);
			table.setWidthPercentage(100);
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Phrase testo = new Phrase(punto.getText(), FontFactory.getFont(
				FontFactory.TIMES, 11));
		PdfPCell cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_CENTER);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.setFixedHeight(20);
		table.addCell(cos);

		String value = "";

		try {
			com.itextpdf.text.Image img;
			if (cloruri.getSelectedIndex() == 1) {
				value = "Critico";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("red.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}
			if (cloruri.getSelectedIndex() == 2) {
				value = "Poco critico";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("yellow.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}
			if (cloruri.getSelectedIndex() == 3) {
				value = "Irrilevante";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("green.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}

		} catch (BadElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		testo = new Phrase(value,
				FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_LEFT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setFixedHeight(20);
		table.addCell(cos);

		value = "";
		try {
			com.itextpdf.text.Image img;
			if (nitrati.getSelectedIndex() == 1) {
				value = "Critico";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("red.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}
			if (nitrati.getSelectedIndex() == 2) {
				value = "Poco critico";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("yellow.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}
			if (nitrati.getSelectedIndex() == 3) {
				value = "Irrilevante";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("green.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}

		} catch (BadElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		testo = new Phrase(value,
				FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_LEFT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setFixedHeight(20);
		table.addCell(cos);

		value = "";
		try {
			com.itextpdf.text.Image img;
			if (solfati.getSelectedIndex() == 1) {
				value = "Critico";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("red.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}
			if (solfati.getSelectedIndex() == 2) {
				value = "Poco critico";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("yellow.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}
			if (solfati.getSelectedIndex() == 3) {
				value = "Irrilevante";
				img = com.itextpdf.text.Image.getInstance(ClassLoader
						.getSystemResource("green.jpg"));
				img.scaleAbsolute(14, 14);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.disableBorderSide(PdfPCell.RIGHT);
				table.addCell(cos);
			}

		} catch (BadElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		testo = new Phrase(value,
				FontFactory.getFont(FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		cos.setHorizontalAlignment(Element.ALIGN_LEFT);
		cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cos.disableBorderSide(PdfPCell.LEFT);
		cos.setFixedHeight(20);
		table.addCell(cos);

		try {

			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return doc;
	}
}
